package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.List;

public class UserServiceImpl implements IUserService {
    private UserDao userDao = new UserImpl();

    /**
     * 1.判断当前用户名是否存在，
     * 如果存在则返回错误信息，请求重新输入
     * 如果不存在则直接保存
     * 2.调用数据层
     *
     * @param userModel
     * @return
     */
    @Override
    public String add(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        首先判断用户是否存在

        UserModel model = userDao.findByUsername(userModel.getUsername());
        if (model != null) {
            return "isHave";
        } else {
            int result = userDao.add(userModel);
            if (result == 1) {
                return "success";
            }
            return "error";
        }

    }

    @Override
    public List<UserModel> findAll() {
//        UserDao userDao = new UserImpl();
        return userDao.findAll();
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        return userDao.findAll(userModel);
    }

    /**
     * 1.拿到验证码，并进行判断验证码是否正确
     * 2.判断当前用户名和密码输入是否正确
     * 1）如果正确则返回提示success
     * 2）如果不正确，则返回用户名或者密码输入错误
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
        String result = "";
        if (userModel.getCode().equals(userModel.getSessionCode())) {
//            UserDao userDao = new UserImpl();
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if (model == null) {
                result = "no_user";
            } else {
                result = "success_code";
            }
        } else {
            result = "error_code";
        }
        return result;
    }

    @Override
    public String del(Integer id) {
//        UserDao userDao = new UserImpl();

        return userDao.del(id) == 1 ? "success" : "error";
    }

    @Override
    public UserModel findById(int id) {

        return userDao.findById(id);
    }

    @Override
    public int getCount(UserModel userModel) {
        return userDao.getCount(userModel);
    }
}
